import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/api/special_list.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';
import 'package:get/get.dart';

class CommonJobApprovalController extends GetxController {
  //作业总页面controller
  final workAllPageController = Get.find<CommonWorkAllController>();

  //作业票id
  late String ticketReceiptId;

  int milliseconds = 0;

  //作业审批节点数据
  List<ExecuteWorkCheckList> workCheckList = [];

  int currentlyProgress = 0; //当前进行中的索引

  @override
  void onInit() {
    super.onInit();
    // 初始化页面列表
    initData();
    // 获取数据气体检测有效期
    if (workAllPageController.receiptType == 1) {
      getTicketReceiptValidDate();
    }
    // 加载作业申请数据,主要为拿到作业时间
    initApplicationDate();
  }

  ///初始化数据
  initData() async {
    //作业票id
    ticketReceiptId = workAllPageController.executeEntity.ticketReceiptId!;
    workCheckList.clear();
    //获取数据-安全措施数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteWorkCheckList>>(Interface.getListCheckById,
        queryParameters: {'ticketReceiptId': workAllPageController.executeEntity.ticketReceiptId}).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.workCheckLists = result.data;
    });
    //赋值
    workCheckList = workAllPageController.executeEntity.workCheckLists!;
    //如果为空,初始化数据
    if (workCheckList.isEmpty) {
      setInitList(workAllPageController.businessType);
    } else {
      //设置显示长度
      currentlyProgress = workCheckList.length - 1;
    }
    update();
  }

  /// 加载作业申请数据,主要为拿到作业时间
  Future<void> initApplicationDate() async {
    //获取数据-作业申请数据
    final res =
        await HttpUtil().generateGetRequest<ExecuteEntity>(Interface.getTicketReceiptDetail, queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.startDate = result.data!.startDate;
    });
    update();
  }

  void setInitList(String type) {
    ///1：动火作业 2：受限空间作业 3：盲板抽堵作业 4：高处作业 5：吊装作业 6：临时用电作业 7：动土作业 8：断路作业
    switch (type) {
      case '1':
        initFireList();
        break;
      case '2':
        initRestrictedList();
        break;
      case '3':
        initBlindPlateList();
        break;
      case '4':
        initHeightList();
        break;
      case '5':
        initHoistingList();
        break;
      case '6':
        initElectricityList();
        break;
      case '7':
        initGroundList();
        break;
      case '8':
        initRoadList();
        break;
    }
  }

  ///动火作业初始数据
  void initFireList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 3
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '安全管理部门意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 4
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '动火审批人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 5
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '动火前,岗位当班班组验票情况');
    update();
  }

  ///受限空间初始数据
  void initRestrictedList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    update();
  }

  ///盲板抽堵初始数据
  void initBlindPlateList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    update();
  }

  ///高处作业初始数据
  void initHeightList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 3
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '审核部门意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 4
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '审批部门意见');
    update();
  }

  ///吊装作业初始数据
  void initHoistingList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业指挥意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 3
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '审核部门意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 4
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '审批部门意见');
    update();
  }

  ///临时用电初始数据
  void initElectricityList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '用电单位意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 3
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '配送电单位意见');
    update();
  }

  ///动土作业初始数据
  void initGroundList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 3
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '有关水、电、汽、工艺、设备、消防、安全等部门会签意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 4
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '审批部门意见');
    update();
  }

  ///断路作业初始数据
  void initRoadList() {
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 1
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '作业负责人意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 2
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '所在单位意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 3
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '消防、安全管理部门意见');
    workCheckList.add(ExecuteWorkCheckList()
      ..sort = 4
      ..ticketReceiptId = ticketReceiptId
      ..checkProject = '审批部门意见');
    update();
  }

  ///更新当前进行中的索引
  void updateCurrentlyProgress() {
    //是否全部数据已完成
    bool isCompleted = true;

    ///循环遍历当前页面列表，如果有未填写的，就更新当前进行中的索引
    for (int i = 0; i < workCheckList.length; i++) {
      if (workCheckList[i].checkUserSign!.isEmpty ||
          // workCheckList[i].checkReason == '' ||
          workCheckList[i].checkDate == '' ||
          workCheckList[i].checkImage!.isEmpty) {
        isCompleted = false;
        currentlyProgress = i;
        break;
      }
    }
    //全部完成后显示所有
    if (isCompleted) {
      currentlyProgress = workCheckList.length - 1;
    }
    update();
  }

  ///获取数据气体检测有效期
  getTicketReceiptValidDate() async {
    //如果要显示才去请求
    final res = await SpecialListApis.getListConfigByTicketReceiptId(ticketReceiptId).execute();
    res.onSuccess((result) {
      if (result.data != null && result.data!.gasDetectionValidDate != 0) {
        milliseconds = result.data!.gasDetectionValidDate!;
        update();
      }
    });
  }
}
